//
//  ThemeManager.m
//  WXWeibo
//
//  Created by apple on 16/5/10.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "ThemeManager.h"



@implementation ThemeManager

+ (instancetype)shareManager {
    
    
    static ThemeManager *instance = nil;
    

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[super allocWithZone:NULL] init];
    });
    return instance;
    
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
   
    return [ThemeManager shareManager];
}

- (id)copy {
    
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _themeName = @"猫爷";
        
        NSString *saveThemeName = [[NSUserDefaults standardUserDefaults] objectForKey:kThemeName];
        if (saveThemeName.length > 0) {
            _themeName = saveThemeName;
        }
        
        _themeDic = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Theme.plist" ofType:nil]];
        [self _loadColorConfigFile];
        
    }
    return self;
}


- (UIImage *)themeImageWithImageName:(NSString *)imageName {
    
    NSString *themePath = [self themePath];
    
    NSString *imagePath = [themePath stringByAppendingPathComponent:imageName];
    
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

    
    return image;
}

- (NSString *)themePath {
    
    NSString *path = self.themeDic[_themeName];

    
    NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:path];
    

    return themePath;
}

- (void)setThemeName:(NSString *)themeName {
    
    if (_themeName != themeName) {
        _themeName = [themeName copy];
        
        [self _loadColorConfigFile];
        [[NSUserDefaults standardUserDefaults] setObject:_themeName forKey:kThemeName];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:kThemeChangeNotification object:nil];
    }
    
}

//加载color plist
- (void)_loadColorConfigFile {
    
    
    NSString *configFilePath = [[self themePath] stringByAppendingPathComponent:@"config.plist"];
    
    _colorDic = [NSDictionary dictionaryWithContentsOfFile:configFilePath];
    
    
}

- (UIColor *)themeColorWithColorName:(NSString *)colorName {
    
    NSDictionary *rgbDic = _colorDic[colorName];
    
    CGFloat r = [rgbDic[@"R"] floatValue];
    CGFloat g = [rgbDic[@"G"] floatValue];
    CGFloat b = [rgbDic[@"B"] floatValue];
    
    CGFloat alpha = rgbDic[@"alpha"] ? [rgbDic[@"alpha"] floatValue] : 1;
    
    
    return [UIColor colorWithRed:r / 255.0 green:g /255.0 blue:b/ 255.0 alpha:alpha];
}

@end
